<?php

// ------------------------------------------------------------------------
// |@Author       : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-04 23:29:01
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-10 14:02:17
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Description  :
// |@----------------------------------------------------------------------
// |@FilePath     : Exception.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn All Rights Reserved.
// ------------------------------------------------------------------------
declare (strict_types=1);

namespace think\admin;

use think\admin\traits\ErrorCodeTrait;
use think\admin\traits\HttpStatusTrait;

/**
 * 自定义异常基类
 * @package think\admin
 */
class Exception extends \think\Exception
{
    use ErrorCodeTrait;
    use HttpStatusTrait;

    /**
     * 错误数据
     * @var mixed
     */
    protected $errorData = null;

    /**
     * HTTP状态码
     * @var int
     */
    protected $httpCode = 500;

    /**
     * 构造函数
     * @param string $message 错误信息
     * @param int $code 错误码
     * @param mixed $data 错误数据
     * @param \Throwable|null $previous 上级异常
     */
    public function __construct(string $message = '', int $code = 0, mixed $data = null, ?\Throwable $previous = null)
    {
        $this->errorData = $data;
        $this->httpCode = $this->getHttpStatusCode($code);

        // 如果没有提供错误信息，则使用错误码对应的默认信息
        if (empty($message) && $code > 0) {
            $message = $this->getErrorMessage($code);
        }

        parent::__construct($message, $code, $previous);
    }

    /**
     * 获取错误数据
     * @return mixed
     */
    public function getErrorData(): mixed
    {
        return $this->errorData;
    }

    /**
     * 获取HTTP状态码
     * @return int
     */
    public function getHttpCode(): int
    {
        return $this->httpCode;
    }
}
